package com.lt.schoolchat.controller;

import com.lt.schoolchat.common.BaseResponse;
import com.lt.schoolchat.common.ErrorCode;
import com.lt.schoolchat.common.ResultUtils;
import com.lt.schoolchat.exception.BusinessException;
import com.lt.schoolchat.model.entity.User;
import com.lt.schoolchat.model.vo.UserVO;
import com.lt.schoolchat.service.CommentService;
import com.lt.schoolchat.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @author: ~Teng~
 * @date: 2023/5/18 20:31
 */
@RestController
@RequestMapping("/comment")
@Slf4j
@Api(tags = "互动接口")
public class CommentController {
    @Resource
    private CommentService commentService;
    @Resource
    private UserService userService;

    @GetMapping("/{id}/like")
    @ApiOperation("点赞小视频")
    public BaseResponse<Integer> like(@PathVariable("id") String videoId, HttpServletRequest request) {
        if (StringUtils.isBlank(videoId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        Integer likeCount = commentService.like(new ObjectId(videoId), userId);
        return ResultUtils.success(likeCount);
    }

    @GetMapping("/{id}/unlike")
    @ApiOperation("取消点赞小视频")
    public BaseResponse<Integer> unlike(@PathVariable("id") String videoId, HttpServletRequest request) {
        if (StringUtils.isBlank(videoId)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        Long userId = loginUser.getId();
        Integer likeCount = commentService.unlike(new ObjectId(videoId), userId);
        return ResultUtils.success(likeCount);
    }

    @PostMapping("/{uid}/userFocus")
    @ApiOperation("用户关注作者")
    public BaseResponse<Boolean> useFocus(@PathVariable("uid") Long uid, HttpServletRequest request) {
        if (uid == null || uid <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        Long userId = loginUser.getId();
        if (userId.equals(uid)) {
            throw new BusinessException(ErrorCode.NOT_FOCUS_ME);
        }
        commentService.useFocus(uid, userId);
        return ResultUtils.success(Boolean.TRUE);
    }

    @PostMapping("/{uid}/useUnFocus")
    @ApiOperation("用户取消关注作者")
    public BaseResponse<Boolean> useUnFocus(@PathVariable("uid") Long uid, HttpServletRequest request) {
        if (uid == null || uid <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        Long userId = loginUser.getId();
        if (userId.equals(uid)) {
            throw new BusinessException(ErrorCode.NOT_FOCUS_ME);
        }
        commentService.useUnFocus(uid, userId);
        return ResultUtils.success(Boolean.TRUE);
    }

    @GetMapping("/findFocus")
    @ApiOperation("查询所有关注我的所有用户")
    public BaseResponse<List<UserVO>> findFocusMeUser(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        Long loginUserId = loginUser.getId();
        List<UserVO> userVOList = commentService.findFocusMeUser(loginUserId);
        return ResultUtils.success(userVOList);
    }
}
